package com.baiyang.server.control.read;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.read.Comments;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.service.read.CommentsService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/commentsOperation")
@Jurisdiction(value = "/commentsOperation")
public class CommentsCon {
	@Autowired
	private CommentsService commentsService = null;

	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "getPage")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Comments> getPage(@RequestBody JSONObject object){
		Letter<Comments> letter = new Letter<Comments>();
		try {
			PageEntity<Comments> result = commentsService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 获取最新评价内容
	 * @param object
	 * @return
	 */
	@Jurisdiction(value = "getNewComment")
	@RequestMapping(value = "/getNewComment",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Comments> getNewComment(@RequestBody JSONObject object){
		Letter<Comments> letter = new Letter<Comments>();
		try {
			String authorId = object.getString("authorId");
			List<Comments> list = commentsService.getComment(authorId);
			letter.setList(list);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件获取数据
	 * @return
	 */
	@Jurisdiction(value = "getCommentRecord")
	@RequestMapping(value = "/getCommentRecord",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Comments> getCommentRecord(@RequestBody JSONObject jsonObject){
		Letter<Comments> letter = new Letter<Comments>();
		try {
			String userId = jsonObject.getString("userId");
			int pageOn = jsonObject.getIntValue("pageOn");
			int pageSize = jsonObject.getIntValue("pageSize");
			int pageStart = (pageOn - 1) * pageSize;
			List<Comments> result = commentsService.getCommentRecord(userId,pageStart,pageSize);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

     /**
	 * 根绝条件获取数据
	 * @return
	 */
    @Jurisdiction(value = "selectList")
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Comments> selectList(@RequestBody Comments comments){
		Letter<Comments> letter = new Letter<Comments>();
		try {

			List<Comments> result = commentsService.selectList(comments);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取一个
	 * @param comments
	 * @return
	 */
	@Jurisdiction(value = "selectOne")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Comments> selectOne(@RequestBody Comments comments){
		Letter<Comments> letter = new Letter<Comments>();
		try {
			Comments obj = commentsService.selectOne(comments);
			letter.setData(obj);
			letter.setDetail("数据获取成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 插入数据
	 * @param comments
	 * @return
	 */
	@Jurisdiction(value = "addComment")
	@RequestMapping(value = "/addComment",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> addComment(@RequestBody Comments comments){
		Letter<String> letter = new Letter<String>();
		comments.setId(DataTool.createUUID());
		Map<String , String> sub = SubjectNotNull.subject(comments);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		try {
			commentsService.addComment(comments);
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 插入数据
	 * @param comments
	 * @return
	 */
	@Jurisdiction(value = "insert")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody Comments comments){
		Letter<String> letter = new Letter<String>();
		comments.setId(DataTool.createUUID());
		Map<String , String> sub = SubjectNotNull.subject(comments);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		try {
			commentsService.insert(comments);
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 批量插入数据
	 * @param commentss
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<Comments> commentss){
		Letter<String> letter = new Letter<String>();
		try {
			commentsService.insertBatch(commentss,letter);
			letter.setResult("SUCCESS");
			letter.setDetail("数据插入成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 * @param comments
	 * @return
	 */
	@Jurisdiction(value = "delete")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody Comments comments){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(comments)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			commentsService.delete(comments);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}
    /**
    * 根据id批量删除
    */
    @Jurisdiction(value = "deletes")
    @RequestMapping(value = "/deletes",produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> deletes(@RequestBody List<Comments> commentss){
        Letter<String> letter = new Letter<String>();
        if(DataTool.isBlank(commentss)) {
            letter.setResult("FAIL");
            letter.setDetail("信息不能为空！");
            return letter;
        }
        try {
			commentsService.deletes(commentss);
            letter.setDetail("数据删除成功");
            letter.setResult("SUCCESS");
        }catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据删除失败！");
        }
        return letter;
    }

	/**
	 * 根绝id更新
	 * @param comments
	 * @return
	 */
	@Jurisdiction(value = "updateById")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody Comments comments){
		Letter<String> letter = new Letter<String>();
		if(comments.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			commentsService.updateById(comments);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;
	}
}
